1 using System;
2 using
System.Collections.Generic;
3 using
System.ComponentModel;
4 using
System.Data;
5 using
System.Drawing;
6 using
System.Linq;
7 using
System.Text;
8 using
System.Windows.Forms;
9 using
System.Data.SqlClient;
10 using
Excel = Microsoft.Office.Interop.Excel;
11 namespace
WarehouseManagementSystem
12 {
13     
public partial class frmCustomersRecord2 : Form
14     {
15        
16         DataTable dtable =
new DataTable();
17         SqlConnection con =
null;
18         DataSet ds =
new DataSet();
19         SqlCommand cmd =
null;
20         DataTable dt =
new DataTable();
21         ConnectionString cs =
new ConnectionString();
22         
public frmCustomersRecord2()
23         {
24             InitializeComponent();
25         }
26         
public void GetData()
27         {
28                 
try{
29                 con =
new SqlConnection(cs.DBConn);
30                 con.Open();
31                 cmd =
new SqlCommand("SELECT RTRIM(CustomerID)as [Customer ID],RTRIM(Customername) as [Customer Name],RTRIM(address) as [Address],RTRIM(city) as [City],RTRIM(ContactNo) as [Contact No.],RTRIM(ContactNo1) as [Contact No. 1],(email) as [Email],(notes) as [Notes] from Customer order by CustomerName", con);
32                 SqlDataAdapter myDA =
new SqlDataAdapter(cmd);
33                 DataSet myDataSet =
new DataSet();
34                 myDA.Fill(myDataSet,
"Customer");
35                 dataGridView1.DataSource = myDataSet.Tables[
"Customer"].DefaultView;
36                 con.Close();
37             }
38             
catch (Exception ex)
39             {
40                 MessageBox.Show(ex.Message,
"Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
41             }
42        }
43
44         
private void frmCustomersRecord_Load(object sender, EventArgs e)
45         {
46             GetData();
47         }
48          
49     
50
51         
private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
52         {
53             
string strRowNumber = (e.RowIndex + 1).ToString();
54             SizeF size = e.Graphics.MeasureString(strRowNumber,
this.Font);
55             
if (dataGridView1.RowHeadersWidth < Convert.ToInt32((size.Width + 20)))
56             {
57                 dataGridView1.RowHeadersWidth = Convert.ToInt32((size.Width +
20));
58             }
59             Brush b = SystemBrushes.ControlText;
60             e.Graphics.DrawString(strRowNumber,
this.Font, b, e.RowBounds.Location.X + 15, e.RowBounds.Location.Y + ((e.RowBounds.Height - size.Height) / 2));
61      
62         }
63
64         
private void dataGridView1_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
65         {
66              
try{
67             DataGridViewRow dr = dataGridView1.SelectedRows[
0];
68             
this.Hide();
69             frmCustomers frm=
new frmCustomers();
70            frm.Show();
71            frm.txtCustomerID.Text = dr.Cells[
0].Value.ToString();
72            frm.txtCustomerName.Text = dr.Cells[
1].Value.ToString();
73            frm.txtAddress.Text = dr.Cells[
2].Value.ToString();
74            frm.txtCity.Text = dr.Cells[
3].Value.ToString();
75            frm.txtContactNo.Text = dr.Cells[
4].Value.ToString();
76            frm.txtContactNo1.Text = dr.Cells[
5].Value.ToString();
77            frm.txtEmail.Text = dr.Cells[
6].Value.ToString();
78            frm.txtNotes.Text = dr.Cells[
7].Value.ToString();
79            frm.btnUpdate.Enabled =
true;
80            frm.btnDelete.Enabled =
true;
81            frm.btnSave.Enabled =
false;
82            frm.txtCustomerName.Focus();
83              }
84         
85             
catch (Exception ex)
86             {
87                 MessageBox.Show(ex.Message,
"Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
88             }
89         }
90
91         
private void txtCustomers_TextChanged(object sender, EventArgs e)
92         {
93             
try
94             {
95                 con =
new SqlConnection(cs.DBConn);
96                 con.Open();
97                 cmd =
new SqlCommand("SELECT RTRIM(CustomerID)as [Customer ID],RTRIM(Customername) as [Customer Name],RTRIM(address) as [Address],RTRIM(city) as [City],RTRIM(ContactNo) as [Contact No.],RTRIM(ContactNo1) as [Contact No. 1],(email) as [Email],(notes) as [Notes] from Customer where CustomerName like '" + txtCustomers.Text + "%' order by CustomerName", con);
98                 SqlDataAdapter myDA =
new SqlDataAdapter(cmd);
99                 DataSet myDataSet =
new DataSet();
100                 myDA.Fill(myDataSet,
"Customer");
101                 dataGridView1.DataSource = myDataSet.Tables[
"Customer"].DefaultView;
102                 con.Close();
103             }
104             
catch (Exception ex)
105             {
106                 MessageBox.Show(ex.Message,
"Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
107             }
108         }
109
110         
private void Button3_Click(object sender, EventArgs e)
111         {
112             
if (dataGridView1.DataSource == null)
113             {
114                 MessageBox.Show(
"Sorry nothing to export into excel sheet..", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
115                 
return;
116             }
117             
int rowsTotal = 0;
118             
int colsTotal = 0;
119             
int I = 0;
120             
int j = 0;
121             
int iC = 0;
122             System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor;
123             Excel.Application xlApp =
new Excel.Application();
124
125             
try
126             {
127                 Excel.Workbook excelBook = xlApp.Workbooks.Add();
128                 Excel.Worksheet excelWorksheet = (Excel.Worksheet)excelBook.Worksheets[
1];
129                 xlApp.Visible =
true;
130
131                 rowsTotal = dataGridView1.RowCount;
132                 colsTotal = dataGridView1.Columns.Count -
1;
133                 
var _with1 = excelWorksheet;
134                 _with1.Cells.Select();
135                 _with1.Cells.Delete();
136                 
for (iC = 0; iC <= colsTotal; iC++)
137                 {
138                     _with1.Cells[
1, iC + 1].Value = dataGridView1.Columns[iC].HeaderText;
139                 }
140                 
for (I = 0; I <= rowsTotal - 1; I++)
141                 {
142                     
for (j = 0; j <= colsTotal; j++)
143                     {
144                         _with1.Cells[I +
2, j + 1].value = dataGridView1.Rows[I].Cells[j].Value;
145                     }
146                 }
147                 _with1.Rows[
"1:1"].Font.FontStyle = "Bold";
148                 _with1.Rows[
"1:1"].Font.Size = 12;
149
150                 _with1.Cells.Columns.AutoFit();
151                 _with1.Cells.Select();
152                 _with1.Cells.EntireColumn.AutoFit();
153                 _with1.Cells[
1, 1].Select();
154             }
155             
catch (Exception ex)
156             {
157                 MessageBox.Show(ex.Message,
"Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
158             }
159             
finally
160             {
161                 
//RELEASE ALLOACTED RESOURCES
162                 System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default;
163                 xlApp =
null;
164             }
165         }
166
167         
private void frmCustomersRecord1_FormClosing(object sender, FormClosingEventArgs e)
168         {
169             
this.Hide();
170             frmCustomers frm =
new frmCustomers();
171             frm.Show();
172         }
173     }
174 }


Gõ tìm kiếm nhanh...